﻿using System;
using System.Collections;
using System.Collections.Specialized;
using EPiServer.Common.Tags;

namespace EPiServer.Templates.RelatePlus.Templates.PageEntity.TagBox
{
    public partial class TagBoxAjax : System.Web.UI.Page
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            Response.ContentType = "text/plain";
            Response.Cache.SetExpires(DateTime.Now.AddDays(-1));

            string query = Request.QueryString["q"];

            int total;
            HybridDictionary tags = new HybridDictionary(false);

            PredefinedTagCollection predefinedTags = TagHandler.GetPredefinedTags();
            foreach (var predefinedTag in predefinedTags)
            {
                if (predefinedTag.Tag.Name.Contains(query))
                {
                    tags.Add(predefinedTag.Tag.ID,predefinedTag.Tag.Name);
                }
            }
            

            TagCollection allTags = TagHandler.GetTags(null, null, 1, 500, out total);

            foreach (Tag t in allTags)
            {
                if (t.Name.Contains(query) && !tags.Contains(t.ID))
                {
                    tags.Add(t.ID, t.Name);
                }
            }


            foreach (DictionaryEntry dictionaryEntry in tags)
            {
                Response.Write(dictionaryEntry.Value + "|" + dictionaryEntry.Key + "\n");
            }
        }
    }
}